// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of .
//   $Id$


// XmlState.h : Declaration of the CXmlState

#pragma once
#include "resource.h"       // main symbols

#include "XForms.h"

#include "vertex.h" //Declares CBoundNode

// CXmlState

class ATL_NO_VTABLE CXmlState : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CXmlState, &CLSID_XmlState>,
	public IDispatchImpl<IXmlState, &IID_IXmlState, &LIBID_XFORMSLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispatchImpl<IXmlStateManager, &__uuidof(IXmlStateManager), &LIBID_XFORMSLib, /* wMajor = */ 1, /* wMinor = */ 16>,
	public IXmlState2
{
public:
	CXmlState()
		//		:m_pNodeMap(0)
		:m_bWholeValid(true)
		,m_bValidate(VARIANT_TRUE)
		,m_bRelevant(VARIANT_TRUE)
	{
	}

	DECLARE_REGISTRY_RESOURCEID(IDR_XMLSTATE)


	BEGIN_COM_MAP(CXmlState)
		COM_INTERFACE_ENTRY(IXmlState)
		COM_INTERFACE_ENTRY(IDispatch)
		COM_INTERFACE_ENTRY(IXmlStateManager)
		COM_INTERFACE_ENTRY(IXmlState2)
	END_COM_MAP()


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease() 
	{
	}

public:
	/*	void initialise(CMapPtrToPtr * pNodeMap)
	{
	m_pNodeMap = pNodeMap;
	}
	*/
public:

	//The following methods are intended to be 
	//	called using XPath, hence the type of 
	//	the first argument.
	//They each return the value of the 
	//	appropriate property  of the 
	//	zeroth node in the nodeset.

	STDMETHOD(relevant)(IXMLDOMNodeList * pNl, BOOL * bIsRelevant);
	STDMETHOD(valid)(IXMLDOMNodeList * pNl,BOOL * bIsValid);
	STDMETHOD(required)(IXMLDOMNodeList * pNl , BOOL * bIsRequired);
	STDMETHOD(readOnly)(IXMLDOMNodeList * pNl, BOOL * bIsReadOnly);

private:
	//	CMapPtrToPtr * m_pNodeMap;
	//If an invalid node has asked for a validity
	//	check and failed, this stores that fact.
	BOOL m_bWholeValid;
	HRESULT GetBoundNode(IXMLDOMNodeList * pNl,long lIndex, CBoundNode *& rpBoundNode);
public:
	STDMETHOD(isOfType)(IXMLDOMNodeList* pNl, BSTR bsURI, BSTR bsName , BOOL * bResult);

public:
	// IXmlStateManager Methods
	STDMETHOD(get_bValidate)(VARIANT_BOOL  * pbValidate);
	STDMETHOD(put_bValidate)(VARIANT_BOOL  bValidate);
	STDMETHOD(isValid)( VARIANT_BOOL *  pbIsValid);
	STDMETHOD(get_bRelevant)(VARIANT_BOOL* pbRelevant);
	STDMETHOD(put_bRelevant)(VARIANT_BOOL bRelevant);

public:
	// IXmlState2
	STDMETHOD(valid)(IXMLDOMNodeList *pNodeList, VARIANT_BOOL bTestAllNodes, VARIANT_BOOL *pbValid);
	STDMETHOD(validOrIrrelevant)(IXMLDOMNodeList *pNodeList, VARIANT_BOOL bTestAllNodes, VARIANT_BOOL *pbValid);

private:
	VARIANT_BOOL m_bValidate;
	VARIANT_BOOL m_bRelevant;
};

OBJECT_ENTRY_AUTO(__uuidof(XmlState), CXmlState)
